<template>
	<view class="content">
		<view v-for="item in tableData" :key="item.id">
			<uni-card :title="item.name" :extra="item.state">
				<view>出题人：{{item.teacher}}</view>
				<view>考试时间：{{item.time}}</view>
				<view v-if="!item.isAnswer">
					<button type="primary" style="margin-top: 10rpx;" @click="sign(item.id)"
						v-if="item.state!='已结束'&&!item.enable">报名</button>
					<button type="primary" style="margin-top: 10rpx;" @click="doExamPaper(item.paperId,item.name,item.id)"
						v-if="item.enable&&item.state!='已结束'&&item.state=='进行中'">开始考试</button>
				</view>
				<view v-if="item.isAnswer">
					<button type="primary" disabled style="margin-top: 10rpx;" >已提交</button>
				</view>
			</uni-card>
		</view>
		<view v-if="tableData.length === 0" style="text-align: center; font-size: 48rpx; height: 100vh; line-height: 100vh; color:rgb(127,127,126) ;">
			最近没有考试哦！
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				tableData: [],
				user: {},

			}
		},
		onLoad() {
			this.user = uni.getStorageSync('user');
			this.loading();
		},
		onShow(){
			this.loading();
		},
		onPullDownRefresh() {
			this.request({
				url: '/exam/page/front',
				data: {
					userId: this.user.id,
				}
			}).then(res => {
				if (res.code === '0') {
					setTimeout(function() {
						uni.stopPullDownRefresh();
						setTimeout(function() {
							uni.showToast({
								title: '加载成功'
							})
						}, 200);
					}, 800);
				}
				this.tableData = res.data;
			})

		},
		methods: {
			loading() {
				this.request({
					url: '/exam/page/front',
					data: {
						userId: this.user.id,
					}
				}).then(res => {
					this.tableData = res.data;
				})
			},
			sign(examId) {
				this.request({
					url: '/sign',
					method: 'POST',
					data: {
						userId: this.user.id,
						examId: examId
					}
				}).then(res => {
					if (res.code === '0') {
						uni.showToast({
							title: '报名成功'
						})
					}
				})
			},
			doExamPaper(paperId,examName,examId) {
				if (paperId) {
					uni.navigateTo({
						url: `/pages/examPaper/examPaper?paperId=${paperId}&examName=${examName}&examId=${examId}`
					});
				}else{
					uni.showToast({
						icon: 'error',
						title: '老师还未出卷'
					});
				}
			}
		}
	}
</script>

<style>
	.content {
		/* display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: aqua; */
	}
</style>
